/*
 * SelectionController.java
 *
 * Created on November 11, 2006, 11:46 PM
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package edu.umn.cs5115.scheduler.framework.gui;

import edu.umn.cs5115.scheduler.framework.KeyValueCodingBase;

/**
 * This class manages a single object selection.  Bind selections in your user
 * interface through this class to propagate the selection to other parts of
 * the program.
 * @author grant
 */
public class SelectionController<E> extends KeyValueCodingBase
{
    /** Key that notifications of changes to the selected object will be sent on. */
    public final static String SELECTION_KEY = "selection";
    /** Stores the current selection. */
    private E selection;
    
    /** Creates a new instance of SelectionController */
    public SelectionController()
    {
    }
    
    /**
     * Get the currently selected object.
     * @return The currently selected object, or null if the selection is empty.
     * @see #setSelection(Object)
     */
    public E getSelection()
    {
        return selection;
    }
    
    /**
     * Set the current selection.  Setting this to a new value will cause change
     * notifications on the key SELECTION_KEY.
     * @param newSelection The new object to select.  Setting this to null 
     * implies an empty selection.
     * @see #getSelection()
     * @see #SELECTION_KEY
     */
    public void setSelection(E newSelection)
    {
        if (newSelection != selection)
        {
            selection = newSelection;
            didChangeValueForKey(SELECTION_KEY);
        }
    }
}
